<?php
namespace app\controller;

use think\Controller;
use Overtrue\PHPOpenCC\OpenCC;
use app\service\R2Service;

class Publishpc extends Controller
{
    private $_url = [
        
        ['https://pcwallpaper.zhhainiao.com/wallpaper/static/list',
        '{"login_info":{},"cate_id":1,"tag_id":null,"page":%page%,"page_size":100,"sort_type":2,"common":{"open_id":"T4cdd25b345164816682c5d350004e4dd","token":"PoaKVlwLkVrRDmBeHayuVZvPew7pwGIXED9m3peh","device_id":"7f18c640d5cb6c80f8eb8cf1569f7f9a","player_version":115,"platform":"pc"}}',
        1],
        ['https://pcwallpaper.zhhainiao.com/wallpaper/static/list',
        '{"login_info":{},"cate_id":5,"tag_id":null,"page":%page%,"page_size":100,"sort_type":2,"common":{"open_id":"T4cdd25b345164816682c5d350004e4dd","token":"PoaKVlwLkVrRDmBeHayuVZvPew7pwGIXED9m3peh","device_id":"7f18c640d5cb6c80f8eb8cf1569f7f9a","player_version":115,"platform":"pc"}}',
        9],
        ['https://pcwallpaper.zhhainiao.com/wallpaper/static/list',
        '{"login_info":{},"cate_id":2,"tag_id":null,"page":%page%,"page_size":100,"sort_type":2,"common":{"open_id":"T4cdd25b345164816682c5d350004e4dd","token":"PoaKVlwLkVrRDmBeHayuVZvPew7pwGIXED9m3peh","device_id":"7f18c640d5cb6c80f8eb8cf1569f7f9a","player_version":115,"platform":"pc"}}',
        2],
        ['https://pcwallpaper.zhhainiao.com/wallpaper/static/list',
        '{"login_info":{},"cate_id":4,"tag_id":null,"page":%page%,"page_size":100,"sort_type":2,"common":{"open_id":"T4cdd25b345164816682c5d350004e4dd","token":"PoaKVlwLkVrRDmBeHayuVZvPew7pwGIXED9m3peh","device_id":"7f18c640d5cb6c80f8eb8cf1569f7f9a","player_version":115,"platform":"pc"}}',
        3], 
        ['https://pcwallpaper.zhhainiao.com/wallpaper/static/list',
        '{"login_info":{},"cate_id":12,"tag_id":null,"page":%page%,"page_size":100,"sort_type":2,"common":{"open_id":"T4cdd25b345164816682c5d350004e4dd","token":"PoaKVlwLkVrRDmBeHayuVZvPew7pwGIXED9m3peh","device_id":"7f18c640d5cb6c80f8eb8cf1569f7f9a","player_version":115,"platform":"pc"}}',
        11], 
        [
        'https://pcwallpaper.zhhainiao.com/wallpaper/static/list',
        '{"login_info":{},"cate_id":6,"tag_id":null,"page":%page%,"page_size":100,"sort_type":2,"common":{"open_id":"T4cdd25b345164816682c5d350004e4dd","token":"PoaKVlwLkVrRDmBeHayuVZvPew7pwGIXED9m3peh","device_id":"7f18c640d5cb6c80f8eb8cf1569f7f9a","player_version":115,"platform":"pc"}}',
        12],
    ];

    public function run_index($category_id){
        $category_ids = array_column($this->_url,2);
        if (!in_array($category_id,$category_ids)){
            echo "category_id {$category_id} not found\n";
            exit();
        }
        $index = array_search($category_id,$category_ids);
		$this->_run($this->_url[$index]);
	}

    private function _run($current){

		$page = 1;
        $spider_source = 'zhhainiao';
		while (1) {
			echo "page {$page} cate_id {$current[2]}\n";
			$url = $current[0];
			$post_base = $current[1];
			$cate_id = $current[2];
			$post = str_replace("%page%", $page, $post_base);
			$contents = curl_post($url,$post);
			$contents = json_decode($contents,true);
			if (empty($contents['data']['list'])) {
				echo "没有数据\n";
				break;
			}
			foreach ($contents['data']['list'] as $value) {
				$title = $value['wname'];
				if (empty($title)) {
					$title = str_replace('#', '', $value['tags_str']);
				}
                $title = removeAllBracketsContent($title);
                $title = str_replace([',','，'], ' ', $title);
				$title = trim($title);
				if (empty($title)) {
					continue;
				}
				$spider_id = $value['id'];
				$exits = db('wallpaper')
                    ->where([
                        'spider_id'=>$spider_id,
                        'spider_source'=>$spider_source])
                    ->find();
				//var_dump($spider_id,$exits);exit;
				
				if (!empty($exits)) {
					echo "title {$title} repeat noindex {$exits['noindex']}\n";
					continue;
				}
				$title = openCC::simplifiedToTaiwan($title);

				$small_jpg_url = $value['small_jpg_url'];
                $mid_jpg_url = $value['mid_jpg_url'];
                $jpg_url = $value['jpg_url'];
				
				$local_small_jpg = download($small_jpg_url);
				$local_mid_jpg = download($mid_jpg_url);
				$local_jpg = download($jpg_url);

                if (empty($local_small_jpg) && empty($local_mid_jpg) && empty($local_jpg)){
                    continue;
                }
                $r2Service = new R2Service();
                $local_small_jpg_full_path = 'res'.$local_small_jpg;
                $local_mid_jpg_full_path = 'res'.$local_mid_jpg;
                $local_jpg_full_path = 'res'.$local_jpg;
                $local_small_jpg = $r2Service->uploadFile($local_small_jpg_full_path);
                $local_mid_jpg = $r2Service->uploadFile($local_mid_jpg_full_path);
                $local_jpg = $r2Service->uploadFile($local_jpg_full_path);
				
				$pic = [
                    'small_jpg'	=> $local_small_jpg,
                    'mid_jpg'	=> $local_mid_jpg,
                    'jpg'	=> $local_jpg,
                ];
                // debug_dump($pic);

                $value['tags_str'] = explode(',', $value['tags_str']);
                $value['tags_str'] = array_unique($value['tags_str']);
                $tag_name = implode(',', $value['tags_str']);
				
                $title_simplify = filter_non_text_chars($title);
                $noindex_wallpaper = db('wallpaper')->where('title',$title)->where('status',1)->where('noindex',0)->find();
                $noindex = 1;
                if (!empty($noindex_wallpaper)){
                    $noindex = 0;
                }
                
                // debug_dump($wallpaper_title_data);
                $id = db('wallpaper')->insertGetId([
                    'title'         => $title,
                    'title_simplify'=> $title_simplify,
                    'type'          => 'desktop',
                    'category_id'   => $cate_id,
                    'tag_name'      => $tag_name,
                    'noindex'       => $noindex,
                    'file_path'     => json_encode($pic,JSON_UNESCAPED_UNICODE),
                    'create_time'   => date('Y-m-d H:i:s'),
                    'update_time'   => date('Y-m-d H:i:s'),
                    'spider_source' => $spider_source,
                    'spider_id'     => $spider_id,
                    'title_id'      => 0,
                ]);

				if (!$id){
                    echo "title {$title} noindex {$noindex} insert failed\n";
                    exit();
                }
               
                echo "title {$title} noindex {$noindex} success\n";
                return true;
			}
			++$page;
		}
	}
}